<?php

/**
 * 现有存量淘宝商品数据有白底图的 全部将首图设置为场景图
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1); //不限制内存
ini_set("error_reporting", E_ALL);//显示所有错误

require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\Wiki as mWiki;
use Doraemon\tools\Log;

try {
    $programStartTime = time();
    //查询符合条件的商品
    $where = [
        'status[!]' => mWiki::STATUS_DEL,
        'channel' => mWiki::CHANNEL_TAOBAO,
        'white_image[!]' => '',
        'scene_img' => '',
        "ORDER" => ['id' => 'DESC'],
    ];
    $maxWikiId = mWiki::get('id', $where) ?? 0;
    $pageSize = 4000;

    $page = ceil($maxWikiId / $pageSize);
    $msgAll = '';
    $i = 0;
    while ($i < $page) {
        $start = $i * $pageSize;
        $end = ($i + 1) * $pageSize;
        unset($where['ORDER']);
        $wikiInfos = mWiki::select(['id', 'cover_img', 'white_image', 'scene_img'],
            array_merge($where, ["id[<>]" => [$start, $end]]));
        $j = 0;
        foreach ($wikiInfos as $wiki) {
            //如果白底图存在不为空  且场景图不存在 为空  则把首图设置为场景图
            if (!empty($wiki['white_image']) && empty($wiki['scene_img'])) {
                mWiki::update(['scene_img' => $wiki['cover_img']], ['id' => $wiki['id']]);
                $j++;
            }
        }
        $i++;
        $msg = '第'.$i."批数据更新完成;共修改".$j.'条数据;'.PHP_EOL;
        echo $msg;
        $msgAll .= $msg;
    }
    $programEndTime = time();
    echo '共消耗时间'.($programEndTime - $programStartTime).PHP_EOL;
    Log::info('syncWikiSceneImg.php执行成功', ['$msgAll' => $msgAll]);
} catch (Exception $exception) {
    Log::error('syncWikiSceneImg.php执行失败', $exception);
}

